//
//  CPShareItem.m
//  ChinaPoem
//
//  Created by 赵俊明 on 2019/3/10.
//  Copyright © 2019 赵俊明. All rights reserved.
//

#import "CPShareItem.h"

@interface CPShareItem()<UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, weak) UICollectionView *collectionView;
@property(nonatomic, strong) UICollectionViewFlowLayout *layout;
@end

@implementation CPShareItem
static NSString *cellID = @"cell";
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
//        self.backgroundColor = [UIColor redColor];
        self.dataSource = [NSMutableArray arrayWithArray:@[@"添加照片",@"01",@"02",@"03",@"04",@"05",@"06",@"07",@"08",@"09",@"10",@"11",@"12",@"13",@"14",@"15"]];
        self.layout = [UICollectionViewFlowLayout new];
        _layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _layout.minimumLineSpacing = 5;
        
        UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectZero collectionViewLayout:_layout];
        collectionView.backgroundColor = [UIColor clearColor];
        collectionView.delegate = self;
        collectionView.dataSource = self;
        collectionView.scrollsToTop = NO;
        collectionView.showsVerticalScrollIndicator = NO;
        collectionView.showsHorizontalScrollIndicator = NO;
        [collectionView registerClass:[CPShareCell class] forCellWithReuseIdentifier:cellID];
        [self addSubview:collectionView];
        self.collectionView = collectionView;
    }
    return self;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return _dataSource.count;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    CPShareCell *cell =  [collectionView dequeueReusableCellWithReuseIdentifier:cellID forIndexPath:indexPath];
    if (!cell) {cell = [[CPShareCell alloc] init];}
    cell.imaName = _dataSource[indexPath.row];
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.DidSelect) {
        self.DidSelect(_dataSource[indexPath.row],(indexPath.row == 0));
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _layout.itemSize = CGSizeMake(self.height-2, self.height-4);
    _collectionView.frame = self.bounds;
}
@end

#pragma mark - **************** CPShareCell
@implementation CPShareCell
- (instancetype)initWithFrame:(CGRect)frame
{
    if ([super initWithFrame:frame]) {
        
//        self.backgroundColor = [UIColor greenColor];
        _items = [[UIImageView alloc] initWithFrame:CGRectZero];
        _items.contentMode = UIViewContentModeScaleAspectFill;
        _items.clipsToBounds = YES;
        [self.contentView addSubview:_items];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    _items.frame = self.bounds;
}

- (void)setImaName:(NSString *)imaName
{
    _imaName = imaName;
    _items.image = [UIImage imageNamed:imaName];
}

@end
